package com.xpgk.mvc.binShopService;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.bentryRegister.CustomerProductSurplus;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productOrgExperience.ProductOrgExperience;
import com.xpgk.mvc.productOutin.ProductOutin;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.productStock.ProductStock;
import com.xpgk.mvc.productStock.ProductStockInfo;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.mvc.statistic.monthTotalStatistic.MonthTotal;
import com.xpgk.mvc.statistic.yearTotalStatistic.YearTotal;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolDateTime;
import com.xpgk.tools.ToolRandoms;

public class BeingServedController extends BaseController {
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BeingServedController.class);

	private BinShopServiceService binShopServiceService = new BinShopServiceService();

	public void index() {
		List<Record> result = binShopServiceService.getView(getCUserOrganIds(), null, null, null);
		String sqlbb = "SELECT * FROM sb_product_orgexperience where organIds =? and bbExperience IS NOT NULL and dataStatus = '1'";
		String sqlkb = "SELECT * FROM sb_product_orgexperience where organIds =? and kbExperience IS NOT NULL and dataStatus = '1'";
		String sqlfr = "SELECT * FROM sb_product_orgexperience where organIds =? and frExperience IS NOT NULL and dataStatus = '1'";
		ProductOrgExperience bb = ProductOrgExperience.dao.findFirst(sqlbb,getCUserOrganIds());
		int n = 4;
		if(bb!=null){
			n++;
			setAttr("bbExp", "1");
		}else{
			setAttr("bbExp", "0");
		}
		ProductOrgExperience kb = ProductOrgExperience.dao.findFirst(sqlkb,getCUserOrganIds());
		if(kb!=null){
			n++;
			setAttr("kbExp", "1");
		}else{
			setAttr("kbExp", "0");
		}
		ProductOrgExperience fr = ProductOrgExperience.dao.findFirst(sqlfr,getCUserOrganIds());
		if(fr!=null){
			n = n+2;
			setAttr("Exp", "1");
		}else{
			setAttr("Exp", "0");
		}
		setAttr("partnum",n);
		setAttr("resultList", result);
		render("servicesing.html");
	}

	public void view() throws UnsupportedEncodingException {
		String name = getPara("name");
		List<Record> result = binShopServiceService.getView(getCUserOrganIds(),name , getPara("phone"),getPara("hybh"));
		setAttr("resultList", result);
		setAttr("customerName",name);
		setAttr("phone", getPara("phone"));
		setAttr("customerHybh", getPara("hybh"));
		render("servicesing.html");
	}
	
	

	public void getCustomerProduct() {
		String cid = getPara("cid");
		String bw = getPara("bw");
		
		double jysy = 0;
		float jymr = 0;
		double gsy = 0;
		float gmr = 0;
		
		if (StringUtils.isNotBlank(bw) && bw.equals("yb")) {

			String orgid = getCUserOrganIds();//
			String codeId = "a07f80b6d9064a2e8357f0f87d21a115";// 瘦身

			List<Record> list = Db
					.find("SELECT ids  ,name  FROM sb_product e WHERE e.typeIds='a07f80b6d9064a2e8357f0f87d21a115'");
			if (list != null && list.size() > 0) {
				for (Record record : list) {
					if (record.getStr("name").contains("精油")) {
						setAttr("jyproductIds", record.getStr("ids"));
						List<Record> list1 = Db
								.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"
										+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){
								if(list1.get(0).get("sy")!=null){jysy = list1.get(0).get("sy");}else{jysy = 0;}
								}else{
									jysy = 0;
								}
							//setAttr("jysy", list1.get(0).get("sy"));
						}else{
							jysy = 0;
							//setAttr("jysy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = '95e2ae17462e4d0eb1dc84bc63bc478a' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("ybExperience")!=null) {
							jymr = list1.get(0).get("ybExperience");
						}else {
							list1 = Db
									.find("SELECT e.experience as  experience FROM sb_product_experience e WHERE e.partIds = '7b83623e24664b68ab46d68e5255c53f' and e.productIds='"
											+ record.getStr("ids") + "'");
							if (list1 != null && list1.size() > 0) {
								jymr = list1.get(0).get("experience");
								//setAttr("jymr", list1.get(0).get("experience"));
							}else{
								jymr = 0;
								//setAttr("jymr", 0);
							}
						}
						
					} else if (record.getStr("name").contains("膏")) {
						setAttr("gproductIds", record.getStr("ids"));
						List<Record> list1 = Db
								.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"
										+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){
								if(list1.get(0).get("sy")!=null){gsy = list1.get(0).get("sy");}else{gsy = 0;}
								}else{
									gsy = 0;
								}
							
							//setAttr("gsy", list1.get(0).get("sy"));
						}else{
							gsy = 0;
							//setAttr("gsy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = 'daaba791f4104ae98504a1bc421e0d8d' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("ybExperience")!=null) {
						
							gmr = list1.get(0).get("ybExperience");
						}else {
						list1 = Db
								.find("SELECT e.experience as  experience  FROM sb_product_experience e WHERE e.partIds = '7b83623e24664b68ab46d68e5255c53f' and e.productIds='"
										+ record.getStr("ids") + "'");
						if (list1 != null && list1.size() > 0) {
							gmr = list1.get(0).get("experience");
							//setAttr("gmr", list1.get(0).get("experience"));
						}else{
							gmr = 0;
							//setAttr("gmr", 0);
						}}
					}

				}

			}

		}

		if (StringUtils.isNotBlank(bw) && bw.equals("dt")) {

			String orgid = getCUserOrganIds();//
			String codeId = "a07f80b6d9064a2e8357f0f87d21a115";// 瘦身

			List<Record> list = Db
					.find("SELECT ids  ,name  FROM sb_product e WHERE e.typeIds='a07f80b6d9064a2e8357f0f87d21a115'");
			if (list != null && list.size() > 0) {
				for (Record record : list) {
					if (record.getStr("name").contains("精油")) {
						setAttr("jyproductIds", record.getStr("ids"));
						List<Record> list1 = Db
								.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"
										+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){jysy = list1.get(0).get("sy");}else{jysy = 0;}
							//setAttr("jysy", list1.get(0).get("sy"));
						}else{
							jysy = 0;
							//setAttr("jysy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = '95e2ae17462e4d0eb1dc84bc63bc478a' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("dtExperience")!=null) {
							jymr = list1.get(0).get("dtExperience");
						}else {
						list1 = Db
								.find("SELECT e.experience as  experience FROM sb_product_experience e WHERE e.partIds = 'e41f214dc9a04bc691b5674f39de0902' and e.productIds='"+ record.getStr("ids") + "'");
						if (list1 != null && list1.size() > 0) {
							jymr = list1.get(0).get("experience");
							//setAttr("jymr", list1.get(0).get("experience"));
						}else{
							jymr = 0;
							//setAttr("jymr", 0);
						}
						}
					} else if (record.getStr("name").contains("膏")) {
						setAttr("gproductIds", record.getStr("ids"));
						List<Record> list1 = Db
								.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"
										+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){gsy = list1.get(0).get("sy");}else{gsy = 0;}
							//setAttr("gsy", list1.get(0).get("sy"));
						}else{
							gsy = 0;
							//setAttr("gsy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = 'daaba791f4104ae98504a1bc421e0d8d' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("dtExperience")!=null) {
							gmr = list1.get(0).get("dtExperience");
						}else {
						list1 = Db
								.find("SELECT e.experience as  experience  FROM sb_product_experience e WHERE e.partIds = 'e41f214dc9a04bc691b5674f39de0902' and e.productIds='"
										+ record.getStr("ids") + "' ");
						if (list1.get(0).get("experience") != null) {
							gmr =  list1.get(0).get("experience");
							//setAttr("gmr", list1.get(0).get("experience"));
						}else{
							gmr = 0;
							//setAttr("gmr", 0);
						}
						}
					}

				}

			}

		}

		if (StringUtils.isNotBlank(bw) && bw.equals("xt")) {

			String orgid = getCUserOrganIds();//
			String codeId = "a07f80b6d9064a2e8357f0f87d21a115";// 瘦身

			List<Record> list = Db
					.find("SELECT ids  ,name  FROM sb_product e WHERE e.typeIds='a07f80b6d9064a2e8357f0f87d21a115'");
			if (list != null && list.size() > 0) {
				for (Record record : list) {
					if (record.getStr("name").contains("精油")) {
						setAttr("jyproductIds", record.getStr("ids"));
						List<Record> list1 = Db
								.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"
										+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){jysy = list1.get(0).get("sy");}else{jysy = 0;}
							//setAttr("jysy", list1.get(0).get("sy"));
						}else{
							jysy = 0;
							//setAttr("jysy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = '95e2ae17462e4d0eb1dc84bc63bc478a' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("xtExperience")!=null) {
							jymr = list1.get(0).get("xtExperience");
						}else {
						list1 = Db
								.find("SELECT e.experience as  experience FROM sb_product_experience e WHERE e.partIds = '3d2a59dd84504ff2841e0ebcc1fa339f' and e.productIds='"
										+ record.getStr("ids") + "' ");
						if (list1 != null && list1.size() > 0) {
							jymr = list1.get(0).get("experience");
							//setAttr("jymr", list1.get(0).get("experience"));
						}else{
							jymr = 0;
							//setAttr("jymr", 0);
						}
						}
					} else if (record.getStr("name").contains("膏")) {
						setAttr("gproductIds", record.getStr("ids"));
						List<Record> list1 = Db
								.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"
										+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){gsy = list1.get(0).get("sy");}else{gsy = 0;}
							//setAttr("gsy", list1.get(0).get("sy"));
						}else{
							gsy = 0;
							//setAttr("gsy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = 'daaba791f4104ae98504a1bc421e0d8d' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("xtExperience")!=null) {
							gmr = list1.get(0).get("xtExperience");
						}else {
						list1 = Db
								.find("SELECT e.experience as  experience  FROM sb_product_experience e WHERE e.partIds = '3d2a59dd84504ff2841e0ebcc1fa339f' and e.productIds='"
										+ record.getStr("ids") + "'  ");
						if (list1 != null && list1.size() > 0) {
							gmr = list1.get(0).get("experience");
							//setAttr("gmr", list1.get(0).get("experience"));
						}else{
							gmr = 0;
							//setAttr("gmr", 0);
						}
						}
					}

				}

			}

		}

		if (StringUtils.isNotBlank(bw) && bw.equals("gb")) {

			String orgid = getCUserOrganIds();//
			String codeId = "a07f80b6d9064a2e8357f0f87d21a115";// 瘦身

			List<Record> list = Db
					.find("SELECT ids  ,name  FROM sb_product e WHERE e.typeIds='a07f80b6d9064a2e8357f0f87d21a115'");
			if (list != null && list.size() > 0) {
				for (Record record : list) {
					if (record.getStr("name").contains("精油")) {
						setAttr("jyproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"	+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){jysy = list1.get(0).get("sy");}else{jysy = 0;}
							//setAttr("jysy", list1.get(0).get("sy"));
						}else{
							jysy = 0;
							//setAttr("jysy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = '95e2ae17462e4d0eb1dc84bc63bc478a' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("gbExperience")!=null) {
							jymr = list1.get(0).get("gbExperience");
						}else {
						list1 = Db.find("SELECT e.experience as  experience FROM sb_product_experience e WHERE e.partIds = '92b836d30f494b8388c84a0175839f70' and e.productIds='"
										+ record.getStr("ids") + "'  ");
						if (list1 != null && list1.size() > 0) {
							jymr = list1.get(0).get("experience");
							//setAttr("jymr", list1.get(0).get("experience"));
						}else{
							jymr = 0;
							//setAttr("jymr", 0);
						}
						}
					} else if (record.getStr("name").contains("膏")) {
						setAttr("gproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){gsy = list1.get(0).get("sy");}else{gsy = 0;}
							//setAttr("gsy", list1.get(0).get("sy"));
						}else{
							gsy = 0;
							//setAttr("gsy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = 'daaba791f4104ae98504a1bc421e0d8d' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0 && list1.get(0).get("gbExperience")!=null) {
							gmr = list1.get(0).get("gbExperience");
						}else {
						list1 = Db.find("SELECT e.experience as  experience  FROM sb_product_experience e WHERE e.partIds = '92b836d30f494b8388c84a0175839f70' and e.productIds='"
										+ record.getStr("ids") + "'  ");
						if (list1 != null && list1.size() > 0) {
							gmr =  list1.get(0).get("experience");
							//setAttr("gmr", list1.get(0).get("experience"));
						}else{
							gmr = 0;
							//setAttr("gmr", 0);
						}
						}
					}

				}

			}

		}
		
		//---------------------------------

		if (StringUtils.isNotBlank(bw) && bw.equals("bb")) {

			String orgid = getCUserOrganIds();//
			String codeId = Code.ss_ids;//"a07f80b6d9064a2e8357f0f87d21a115";// 瘦身

			List<Record> list = Db
					.find("SELECT ids  ,name  FROM sb_product e WHERE e.typeIds='a07f80b6d9064a2e8357f0f87d21a115'");
			if (list != null && list.size() > 0) {
				for (Record record : list) {
					if (record.getStr("name").contains("精油")) {
						setAttr("jyproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"	+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){jysy = list1.get(0).get("sy");}else{jysy = 0;}
							//setAttr("jysy", list1.get(0).get("sy"));
						}else{
							jysy = 0;
							//setAttr("jysy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = '95e2ae17462e4d0eb1dc84bc63bc478a' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0) {
						if(list1.get(0).get("bbExperience")!=null){
							jymr = list1.get(0).get("bbExperience");
						}}else {
						
							jymr = 0;
							//setAttr("jymr", 0);
						
						}
					} else if (record.getStr("name").contains("膏")) {
						setAttr("gproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){gsy = list1.get(0).get("sy");}else{gsy = 0;}
							//setAttr("gsy", list1.get(0).get("sy"));
						}else{
							gsy = 0;
							//setAttr("gsy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = 'daaba791f4104ae98504a1bc421e0d8d' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0) {
						if(list1.get(0).get("bbExperience")!=null){
							gmr = list1.get(0).get("bbExperience");
						}}else {
						
							gmr = 0;
							//setAttr("gmr", 0);
						
						}
					}

				}

			}

		}
		
		
		

		if (StringUtils.isNotBlank(bw) && bw.equals("kb")) {

			String orgid = getCUserOrganIds();//
			String codeId = Code.ss_ids;//"a07f80b6d9064a2e8357f0f87d21a115";// 瘦身

			List<Record> list = Db
					.find("SELECT ids  ,name  FROM sb_product e WHERE e.typeIds='a07f80b6d9064a2e8357f0f87d21a115'");
			if (list != null && list.size() > 0) {
				for (Record record : list) {
					if (record.getStr("name").contains("精油")) {
						setAttr("jyproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"	+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){jysy = list1.get(0).get("sy");}else{jysy = 0;}
							//setAttr("jysy", list1.get(0).get("sy"));
						}else{
							jysy = 0;
							//setAttr("jysy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = '95e2ae17462e4d0eb1dc84bc63bc478a' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0) {
						if(list1.get(0).get("kbExperience")!=null){
							jymr = list1.get(0).get("kbExperience");
						}}else {
						
							jymr = 0;
							//setAttr("jymr", 0);
						
						}
					} else if (record.getStr("name").contains("膏")) {
						setAttr("gproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){gsy = list1.get(0).get("sy");}else{gsy = 0;}
							//setAttr("gsy", list1.get(0).get("sy"));
						}else{
							gsy = 0;
							//setAttr("gsy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = 'daaba791f4104ae98504a1bc421e0d8d' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0) {
						if(list1.get(0).get("kbExperience")!=null){
							gmr = list1.get(0).get("kbExperience");
						}}else {
						
							gmr = 0;
							//setAttr("gmr", 0);
						
						}
					}

				}

			}

		}
		
		

		if (StringUtils.isNotBlank(bw) && bw.equals("zfr")||StringUtils.isNotBlank(bw) && bw.equals("yfr")) {

			String orgid = getCUserOrganIds();//
			String codeId = Code.ss_ids;//"a07f80b6d9064a2e8357f0f87d21a115";// 瘦身

			List<Record> list = Db
					.find("SELECT ids  ,name  FROM sb_product e WHERE e.typeIds='a07f80b6d9064a2e8357f0f87d21a115'");
			if (list != null && list.size() > 0) {
				for (Record record : list) {
					if (record.getStr("name").contains("精油")) {
						setAttr("jyproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"	+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){jysy = list1.get(0).get("sy");}else{jysy = 0;}
							//setAttr("jysy", list1.get(0).get("sy"));
						}else{
							jysy = 0;
							//setAttr("jysy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = '95e2ae17462e4d0eb1dc84bc63bc478a' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0) {
						if(list1.get(0).get("frExperience")!=null){
							jymr = list1.get(0).get("frExperience");
						}}else {
						
							jymr = 0;
							//setAttr("jymr", 0);
						
						}
					} else if (record.getStr("name").contains("膏")) {
						setAttr("gproductIds", record.getStr("ids"));
						List<Record> list1 = Db.find("SELECT SUM(IFNULL(totalExperience,0))  sy FROM sb_customer_product  WHERE  customerID = ?  AND  productID ='"+ record.getStr("ids") + "' ", cid);
						if (list1 != null && list1.size() > 0) {
							if(list1.get(0).get("sy")!=null){gsy = list1.get(0).get("sy");}else{gsy = 0;}
							//setAttr("gsy", list1.get(0).get("sy"));
						}else{
							gsy = 0;
							//setAttr("gsy", 0);
						}
						list1 = Db.find("select * from sb_product_orgexperience where organIds = ? and productIds = 'daaba791f4104ae98504a1bc421e0d8d' and dataStatus = '1' ",orgid);
						if (list1 != null && list1.size() > 0) {
						if(list1.get(0).get("frExperience")!=null){
							gmr = list1.get(0).get("frExperience");
						}}else {
						
							gmr = 0;
							//setAttr("gmr", 0);
						
						}
					}

				}

			}

		}
		setAttr("jysy", jysy);
		setAttr("jymr",jymr);
		setAttr("gsy", gsy);
		setAttr("gmr", gmr);
		
		setAttr("cid", cid);
		setAttr("bw", bw);
		String sid = getPara("sid");
		setAttr("sid", sid);
		setAttr("mima", getPara("mima"));
		render("customerProduct.html");
	}

	@Before(Tx.class)
	public void updateSer() throws ParseException {
	try {
		Map<String, Object> m = new HashMap<String, Object>();
		String sf = getPara("sf");
		String bw = getPara("bw");
		String mima = getPara("mima");
		String cid = getPara("cid");
		String sid = getPara("sid");
		String yt = getPara("yt");
		log.info("药贴：111111111111111111111111111111" +"-------------------"+ yt+"-------------------"+cid );
		System.out.println("111111111111111111111111111111"+"-------------------"+yt+"-------------------"+cid);
		String ytid = Code.proYt_ids;
		// 药贴 产品表 c3df895ee3b24d1193cb8156edc665b7
		//结束服务，药贴出库
		if (StringUtils.isNotBlank(yt)) {
			log.info("药贴：222222222222222222222222222222222222222222"+"-------------------"+ yt+"-------------------"+cid);
			if (Integer.parseInt(yt) > 0) {
				log.info("药贴：3333333333333333333333"+"-------------------"+ yt+"-------------------"+cid);
				Product p = Product.dao.findById(ytid);
				log.info("药贴：444444444444444"+p.getStr(Product.column_name));
				String pstSql = getSqlMy(ProductStock.sqlId_selPSByOidsAndPids);
				ProductStock pst = ProductStock.dao.findFirst(pstSql, getCUserOrganIds(), p.getStr(Product.column_ids));
				if (pst != null && pst.getFloat(ProductStock.column_num) >= 1) {
					CustomerService cd = CustomerService.dao.findById(sid);
					log.info("服务id：00000000000000000"+"-------------------"+ sid+"-------------------"+cid);
					log.info("服务：88888888888888888888"+"-------------------"+ cd.getStr(CustomerService.column_ids)+"-------------------"+cid);
					
					if (cd != null) {
						log.info("药贴：66666666666666"+"-------------------"+ yt+"-------------------"+cid);
						cd.set(CustomerService.column_plasterCount, yt);
						Date date = new Date();
						Date startTime = cd.getDate(CustomerService.column_startTime);
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						if(sdf.format(date).equals(sdf.format(startTime))) {  
							 cd.set(CustomerService.column_endTime, date);
						 }else{
							 cd.set(CustomerService.column_endTime, sdf.parse(sdf.format(startTime)+" 21:00:00"));
						 }
						
						cd.set(CustomerService.column_updateDate, new Date());
						cd.set(CustomerService.column_updateId, getCUserIds());
						m.put("msg", cd.update());
						if(cd.getStr(CustomerService.column_plasterCount)!=null){
							log.info("结束服务时药贴：9999999999999999999999"+"-------------------"+cd.getStr(CustomerService.column_plasterCount)+"-------------------"+cid);
						}else{
							log.info("结束服务时药贴：--------------------------------空的啊"+cid);
						}
					}else{
						log.info("药贴：55555555555555555555555555"+"-------------------"+yt+"-------------------"+cid);
					}
					// 保存药贴出库记录
					ProductOutin po = new ProductOutin();
					po.set(ProductOutin.column_ids, ToolRandoms.getUuid(true))
							.set(ProductOutin.column_organIds, getCUserOrganIds())
							.set(ProductOutin.column_typeIds, p.getStr(Product.column_typeIds))
							.set(ProductOutin.column_productIds, p.getStr(Product.column_ids))
							.set(ProductOutin.column_number, Integer.parseInt(yt)).set(ProductOutin.column_price, 0)
							.set(ProductOutin.column_date, new Date()).set(ProductOutin.column_style, "1")
							.set(ProductOutin.column_createDate, new Date())
							.set(ProductOutin.column_createId, getCUserIds())
							.set(ProductOutin.column_userIds, getCUserIds()).set(ProductOutin.column_gxsj, new Date());
					if (!po.save()) {
						log.debug("保存" + p.getStr(Product.column_name) + "出库记录失败！！");
						throw new RuntimeException("保存" + p.getStr(Product.column_name) + "出库记录失败！！");
					}
					// 更新产品库存
					// 根据日期和机构id,产品id查出当日是否有库存记录
					String pstInsql = getSqlMy(ProductStockInfo.sqlId_selByOidsAndPidsAndTime);
					ProductStockInfo psi = ProductStockInfo.dao.findFirst(pstInsql, getCUserOrganIds(),
							p.getStr(Product.column_ids), new Date());

					float num1 = pst.getFloat(ProductStock.column_num) - Float.parseFloat(yt);
					pst.set(ProductStock.column_num, num1).set(ProductStock.column_gxsj, new Date());
					if (!pst.update()) {
						log.debug("更新产品总库存失败");
						throw new RuntimeException("更新产品总库存失败!");
					}
					if (psi != null) {
						// 库存详细信息减少库存
						float num = psi.getFloat(ProductStockInfo.column_stock) - Float.parseFloat(yt);
						psi.set(ProductStockInfo.column_stock, num).set(ProductStockInfo.column_gxsj, new Date())
								.set(ProductOutin.column_updateDate, new Date())
								.set(ProductOutin.column_updateId, getCUserIds()).update();
					} else {
						ProductStockInfo psInfo = new ProductStockInfo();
						String psinfoids = ToolRandoms.getUuid(true);
						psInfo.set(ProductStockInfo.column_ids, psinfoids)
								.set(ProductStockInfo.column_organids, getCUserOrganIds())
								.set(ProductStockInfo.column_productID, p.getStr(Product.column_ids))
								.set(ProductStockInfo.column_type, p.getStr(Product.column_typeIds))
								.set(ProductStockInfo.column_stock, pst.getFloat(ProductStock.column_num))
								.set(ProductOutin.column_createDate, new Date())
								.set(ProductOutin.column_createId, getCUserIds())
								.set(ProductStockInfo.column_gxsj, new Date());
						if (!psInfo.save()) {
							log.debug("保存产品库存记录失败！！");
							throw new RuntimeException("保存产品库存记录失败!");
						}
					}
				} else {
					log.debug(p.getStr(Product.column_name) + "库存不够，无法购买！！");
					// throw new RuntimeException(p.getStr(Product.column_name)
					// + "库存不够，请先入库！！");
					m.put("msg", p.getStr(Product.column_name) + "库存不够，请先入库！！");
				}

			}else if (Integer.parseInt(yt) == 0) {
				CustomerService cd = CustomerService.dao.findById(sid);
				if (cd != null) {
					cd.set(CustomerService.column_plasterCount, yt);
					Date date = new Date();
					Date startTime = cd.getDate(CustomerService.column_startTime);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					if(sdf.format(date).equals(sdf.format(startTime))) {  
						 cd.set(CustomerService.column_endTime, date);
					 }else{
						 cd.set(CustomerService.column_endTime, sdf.parse(sdf.format(startTime)+" 21:00:00"));
					 }
					
					cd.set(CustomerService.column_updateDate, new Date());
					cd.set(CustomerService.column_updateId, getCUserIds());
					m.put("msg", cd.update());
					
					
				}
			}
			log.info("药贴：77777777777777777777777777"+"-------------------"+yt+"-------------------"+cid);
		}
		//保存手法
		if (StringUtils.isNotBlank(sf) && sf.length() == 2) {
			User u = updateSerTool(sf ,getCUserOrganIds());
			if (u != null) {
				CustomerService cd = CustomerService.dao.findById(sid);
				if (cd != null) {
					cd.set(CustomerService.column_isPlaster, "1");
					cd.set(CustomerService.column_userID, u.get("ids"));
					cd.set(CustomerService.column_plasterServier, u.get("ids"));
					cd.set(CustomerService.column_updateDate, new Date());
					cd.set(CustomerService.column_updateId, getCUserIds());
					m.put("user", u);
					m.put("msg", cd.update());

				}
			}
		}
		CustomerService cd = CustomerService.dao.findById(sid);
		DailyNursingTotal  dnt = DailyNursingTotal.dao.getDnt(getCUserOrganIds(),cd.getDate(CustomerService.column_startTime));
		//保存精护部位
		
		if (StringUtils.isNotBlank(mima) && mima.length() == 2) {
			String tsy="";
			boolean flag = true;
			User u = updateSerTool(mima ,getCUserOrganIds());
			CustomerProduct cpj = CustomerProduct.dao.findFirst("SELECT *   FROM sb_customer_product  WHERE  customerID = '" + cid + "'  AND  productID ='" + getPara("jyproductIds") + "' and totalExperience >= '"+getPara("jyusage")+"'  ORDER BY totalExperience DESC LIMIT 1");
			CustomerProduct cpg = CustomerProduct.dao.findFirst("SELECT *   FROM sb_customer_product  WHERE  customerID = '" + cid + "'  AND  productID ='" + getPara("gproductIds") + "' and totalExperience >= '"+getPara("gusage")+"'   ORDER BY totalExperience DESC LIMIT 1");
			//查询顾客在当日有无产品明细存在
			/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			CustomerProductSurplus  cpsj=CustomerProductSurplus.dao.getCustomerProductSurplus(cid,  getPara("jyproductIds"), getCUserOrganIds(), sdf.format(new Date()));
			CustomerProductSurplus  cpsg=CustomerProductSurplus.dao.getCustomerProductSurplus(cid,   getPara("gproductIds"), getCUserOrganIds(), sdf.format(new Date()));*/
			/*CustomerProductSurplus cpsj = CustomerProductSurplus.dao.findFirst("SELECT *   FROM sb_customer_product  WHERE  customerID = '" + cid + "'  AND  productID ='" + getPara("jyproductIds") + "' and totalExperience >= '"+getPara("jyusage")+"'  ORDER BY totalExperience DESC LIMIT 1");
			CustomerProductSurplus cpsg = CustomerProductSurplus.dao.findFirst("SELECT *   FROM sb_customer_product  WHERE  customerID = '" + cid + "'  AND  productID ='" + getPara("gproductIds") + "' and totalExperience >= '"+getPara("gusage")+"'   ORDER BY totalExperience DESC LIMIT 1");*/
			if(u!=null){
				if(cpj==null && Float.parseFloat(getPara("jyusage"))>0){
					flag=false;
				}
				if(cpg==null && Float.parseFloat(getPara("gusage"))>0){
					flag=false;
				}
				if(flag){
					if(cpj != null ) {
						Float f = cpj.getFloat("totalexperience") - Float.parseFloat(getPara("jyusage"));
						cpj.set(CustomerProduct.column_totalExperience, f);
						cpj.update();
					}else{
						if(Float.parseFloat(getPara("jyusage"))>0){
							tsy += "精油不够用了，请续产品！";
						}
					
					}
					if(cpg != null) {
						Float f = cpg.getFloat("totalexperience") - Float.parseFloat(getPara("gusage"));
						cpg.set(CustomerProduct.column_totalExperience, f);
						cpg.update();
					}else{
						if(Float.parseFloat(getPara("gusage"))>0){
							tsy += "膏不够用了，请续产品！";
						}
						
					}
					
				}else{
					tsy = "产品不够用了，请续产品！";
				}
				m.put("msg",tsy);
						if((cpg != null||Float.parseFloat(getPara("gusage"))==0)&&(cpj != null)||Float.parseFloat(getPara("jyusage"))==0){
							
							ServiceShaping  ss = ServiceShaping.dao.findFirst("select *  from   sb_customer_service_shaping   where   serviceIds  = ? and dataStatus in ('1','2') ",sid);
							if(ss!=null){
								if (bw.endsWith("yb")) {
									ss.set(ServiceShaping.column_ybjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_ybgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_yb, u.getStr("ids"));
								}else  if (bw.endsWith("dt")) {
									ss.set(ServiceShaping.column_dtjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_dtgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_dt, u.getStr("ids"));
								}else  if (bw.endsWith("xt")) {
									ss.set(ServiceShaping.column_xtjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_xtgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_xt, u.getStr("ids"));
								}else  if (bw.endsWith("gb")) {
									ss.set(ServiceShaping.column_gbjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_gbgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_gb, u.getStr("ids"));
								}else  if (bw.endsWith("bb")) {
									ss.set(ServiceShaping.column_bbjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_bbgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_bb, u.getStr("ids"));
								}else  if (bw.endsWith("kb")) {
									ss.set(ServiceShaping.column_kbjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_kbgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_kb, u.getStr("ids"));
								}else  if (bw.endsWith("zfr")) {
									ss.set(ServiceShaping.column_zfrjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_zfrgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_zfr, u.getStr("ids"));
								}else  if (bw.endsWith("yfr")) {
									ss.set(ServiceShaping.column_yfrjyusage, getPara("jyusage"));
									ss.set(ServiceShaping.column_yfrgusage, getPara("gusage"));
									ss.set(ServiceShaping.column_yfr, u.getStr("ids"));
								}
								m.put("msg", ss.update());
							}else{
								ServiceShaping cs = new ServiceShaping();
								cs.set(ServiceShaping.column_customerIds, cid);
								cs.set(ServiceShaping.column_createDate, new Date());
								cs.set(ServiceShaping.column_createId, getCUserIds());
								cs.set(ServiceShaping.column_organIds, getCUserOrganIds());
								cs.set(ServiceShaping.column_serviceIds, sid);
								if (bw.endsWith("yb")) {
									cs.set(ServiceShaping.column_ybjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_ybgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_yb, u.getStr("ids"));
								}else  if (bw.endsWith("dt")) {
									cs.set(ServiceShaping.column_dtjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_dtgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_dt, u.getStr("ids"));
								}else  if (bw.endsWith("xt")) {
									cs.set(ServiceShaping.column_xtjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_xtgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_xt, u.getStr("ids"));
								}else  if (bw.endsWith("gb")) {
									cs.set(ServiceShaping.column_gbjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_gbgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_gb, u.getStr("ids"));
								}else  if (bw.endsWith("bb")) {
									cs.set(ServiceShaping.column_bbjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_bbgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_bb, u.getStr("ids"));
								}else  if (bw.endsWith("kb")) {
									cs.set(ServiceShaping.column_kbjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_kbgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_kb, u.getStr("ids"));
								}else  if (bw.endsWith("zfr")) {
									cs.set(ServiceShaping.column_zfrjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_zfrgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_zfr, u.getStr("ids"));
								}else  if (bw.endsWith("yfr")) {
									cs.set(ServiceShaping.column_yfrjyusage, getPara("jyusage"));
									cs.set(ServiceShaping.column_yfrgusage, getPara("gusage"));
									cs.set(ServiceShaping.column_yfr, u.getStr("ids"));
								}
								m.put("msg", cs.save());
							}
							 
							if(cd != null && cd.getStr(CustomerService.column_isShaping) == null)
							{
								//保存日综合查询使用产品人数
//								DailyNursingTotal  dnt = DailyNursingTotal.dao.getDnt(getCUserOrganIds(), new Date());
								if(dnt != null)
								{
									dnt.set(DailyNursingTotal.column_sycprs, dnt.getInt(DailyNursingTotal.column_sycprs)+1);
									CustomerDetail cdet = CustomerDetail.dao.findFirst("SELECT * FROM sb_customer_detail WHERE customerIds = ? and dataStatus in ('1','2')",cid);
									if(ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_y).equals(ToolDateTime.format(new Date(), ToolDateTime.pattern_y))){
										dnt.set(DailyNursingTotal.column_xgkjhrs, dnt.getInt(DailyNursingTotal.column_xgkjhrs)+1);
									}
								}
//								else
//								{
//									dnt = new DailyNursingTotal();
//									dnt.set(DailyNursingTotal.column_organids, getCUserOrganIds())
//										.set(DailyNursingTotal.column_date, ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd))
//										.set(DailyNursingTotal.column_sycprs,1);
//									double  dcbl = ((double)dnt.getInt(DailyNursingTotal.column_sycprs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
//									dnt.set(DailyNursingTotal.column_sycprszb,new DecimalFormat("#.00").format(dcbl*100));
//									if(!dnt.save())
//									{
//										log.debug("保存日综合查询表达标人数失败！！");
//										throw new RuntimeException("保存日综合查询表达标人数失败!");
//									}
//								}



							}
							if(cd !=null){
								cd.set(CustomerService.column_isShaping, "1");
								cd.update();
							}
						}
			}else{
				m.put("msg","验证码错误！"); ;
			}
			
		}
		if(dnt != null)
		{
			double  dcbl = ((double)dnt.getInt(DailyNursingTotal.column_sycprs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
			dnt.set(DailyNursingTotal.column_sycprszb,new DecimalFormat("#.00").format(dcbl*100));
			if(!dnt.update())
			{
				log.debug("更新日综合查询表使用产品人数失败！！");
				throw new RuntimeException("更新日综合查询表使用产品人数失败!");
			}
		}
	
	
		renderJson(m);
	} catch (Exception e) {
		e.printStackTrace();
	}
		
		
	}

	public static User updateSerTool(String verificationCode,String orgId) {
		// 1.取用户
		User user = User.dao.findFirst("SELECT * FROM sys_user WHERE verificationCode = ? and organizationids =? and status ='1' ", verificationCode,orgId);
		return user;
	}

	public static String geth6w(String str) {
		return str.substring(str.length() - 6).trim();
	}
	/**
	 * 正在服务 查看备注
	 */
	public void lookBz(){
		String ids=getPara("ids");
		String yz=getPara("yz");
		CustomerService c=new CustomerService();
		c=c.dao().findById(ids);
		String bz=c.get("bz");
		setAttr("ids",c.getStr("ids"));
		if(bz!=null){
			setAttr("bz",bz);
		}else{
			setAttr("bz","");
		}
		if(yz.equals("1")){
			setAttr("yz",1);
		}else{
			setAttr("yz",2);
		}
		render("customerBz.html");
	}
	
	
	
	/**
	 * 正在服务 保存备注
	 */
	public void saveBz(){
		String ids=getPara("ids");
		String bz=getPara("bz");
		//获取当前登陆人员id
		String  userid=getCUserIds();
		CustomerService c=new CustomerService();
		c=c.dao().findById(ids);
		boolean fag=c.set("bz",bz).set("updateId", userid).set("updateDate", new Date()).update();
		if(fag){
			renderJson("status","200");
		}else{
			renderJson("status","300");
		}
	}
	
	
	
	
	
	
	
	@Before(Tx.class)
	public void updateYt() throws ParseException {
	try {
		Map<String, Object> m = new HashMap<String, Object>();
		String sf = getPara("sf");
		String bw = getPara("bw");
		String mima = getPara("mima");
		String cid = getPara("cid");
		String sid = getPara("sid");
		String yt ="0";
		String ytid = "c3df895ee3b24d1193cb8156edc665b7";
		// 药贴 产品表 c3df895ee3b24d1193cb8156edc665b7
		//结束服务，药贴出库
		if (StringUtils.isNotBlank(yt)) {
			if (Integer.parseInt(yt) == 0) {
				CustomerService cd = CustomerService.dao.findById(sid);
				if (cd != null) {
					cd.set(CustomerService.column_plasterCount, yt);
					m.put("msg", cd.update());
				}
			}
		}

		renderJson(m);
	} catch (Exception e) {
		e.printStackTrace();
	}
		
		
	}
}
